package desktop;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

class ScoreTable extends JPanel {
    private int N;
    private List<JLabel> score1;
    private List<JLabel> score2;
    private int fontSize = 40;
    private JPanel UI;

    ScoreTable(int n){
        N = n;
        score1 = new ArrayList<>();
        score2 = new ArrayList<>();
        this.setLayout(new GridLayout(1,1,0,0));
        UI = initTable(n);
        this.add(UI);
    }

    private JPanel initTable(int n){ //n场比赛
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(3,1,0,0));

        JPanel jPanel1 = new JPanel(); //表头
        jPanel1.setLayout(new GridLayout(1,n+2,0,0));
        JLabel t = new JLabel("组别", JLabel.CENTER);
        t.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        t.setFont(new Font("宋体",Font.PLAIN,fontSize));
        t.setOpaque(true);
        t.setBackground(Color.GRAY);
        jPanel1.add(t);
        for(int i=1;i<=n;i++) {
            t = new JLabel(i + "", JLabel.CENTER);
            t.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            t.setFont(new Font("宋体",Font.PLAIN,fontSize));
            t.setOpaque(true);
            t.setBackground(Color.LIGHT_GRAY);
            jPanel1.add(t);
        }
        t = new JLabel("总分", JLabel.CENTER);
        t.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        t.setFont(new Font("宋体",Font.PLAIN,fontSize));
        t.setOpaque(true);
        t.setBackground(Color.GREEN);
        jPanel1.add(t);

        JPanel jPanel2 = new JPanel();
        jPanel2.setLayout(new GridLayout(1,n+2,0,0));
        t = new JLabel("红队", JLabel.CENTER);
        t.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        t.setFont(new Font("宋体",Font.PLAIN,fontSize));
        t.setOpaque(true);
        t.setBackground(Color.RED);
        jPanel2.add(t);
        if(score1.size()==0) {
            for (int i = 1; i <= n; i++) {
                t = new JLabel("", JLabel.CENTER);
                t.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                t.setFont(new Font("宋体", Font.PLAIN, fontSize));
                t.setOpaque(true);
                t.setBackground(Color.RED);
                jPanel2.add(t);
                score1.add(t);
            }
            t = new JLabel("", JLabel.CENTER);
            t.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            t.setFont(new Font("宋体", Font.PLAIN, fontSize));
            t.setOpaque(true);
            t.setBackground(Color.GREEN);
            jPanel2.add(t);
            score1.add(t);
        }else{
            for(JLabel tt:score1){
                tt.setFont(new Font("宋体", Font.PLAIN, fontSize));
                jPanel2.add(tt);
            }
        }

        JPanel jPanel3 = new JPanel();
        jPanel3.setLayout(new GridLayout(1,n+2,0,0));
        t = new JLabel("黄队", JLabel.CENTER);
        t.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        t.setFont(new Font("宋体",Font.PLAIN,fontSize));
        t.setOpaque(true);
        t.setBackground(Color.YELLOW);
        jPanel3.add(t);
        if(score2.size()==0) {
            for (int i = 1; i <= n; i++) {
                t = new JLabel("", JLabel.CENTER);
                t.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                t.setFont(new Font("宋体", Font.PLAIN, fontSize));
                t.setOpaque(true);
                t.setBackground(Color.YELLOW);
                jPanel3.add(t);
                score2.add(t);
            }
            t = new JLabel("", JLabel.CENTER);
            t.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            t.setFont(new Font("宋体", Font.PLAIN, fontSize));
            t.setOpaque(true);
            t.setBackground(Color.GREEN);
            jPanel3.add(t);
            score2.add(t);
        }else{
            for(JLabel tt:score2){
                tt.setFont(new Font("宋体", Font.PLAIN, fontSize));
                jPanel3.add(tt);
            }
        }

        jPanel.add(jPanel1);
        jPanel.add(jPanel2);
        jPanel.add(jPanel3);
        return jPanel;
    }

    void updateScore(boolean team, int match, int score){ //队伍（T红F黄），赛局
        if(team)
            score1.get(match-1).setText(score+"");
        else
            score2.get(match-1).setText(score+"");
    }

    int getScore(boolean team, int match) { //队伍（T红F黄），赛局
        String score = "";
        if(team)
            score = score1.get(match-1).getText();
        else
            score = score2.get(match-1).getText();
        if(score.equals(""))
            return 0;
        else
            return Integer.parseInt(score);
    }

    void showTotal(){ //计算总分
        if(score1.get(score1.size()-1).getText().equals("")) {
            int total = 0;
            String t;
            for (int i = 0; i < score1.size() - 1; i++) {
                JLabel score = score1.get(i);
                t = score.getText();
                if (!t.equals(""))
                    total += Integer.parseInt(t);
            }
            score1.get(score1.size() - 1).setText(total + "");
            total = 0;
            for (int i = 0; i < score2.size() - 1; i++) {
                JLabel score = score2.get(i);
                t = score.getText();
                if (!t.equals(""))
                    total += Integer.parseInt(t);
            }
            score2.get(score2.size() - 1).setText(total + "");
        }else{
            score1.get(score1.size() - 1).setText("");
            score2.get(score2.size() - 1).setText("");
        }
    }

    void setFontSize(int fontSize){
        this.fontSize = fontSize;
        this.remove(UI);
        UI = initTable(N);
        this.add(UI);
        this.updateUI();
    }

    int getFontSize(){
        return fontSize;
    }
}
